<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="title"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-form-item label="用户名称">
            <el-input
              v-model="tableOptions.queryForm.userName"
              placeholder="用户名称"
            />
          </el-form-item>
          <el-form-item label="用户账号">
            <el-input
              v-model="tableOptions.queryForm.userCode"
              placeholder="用户账号"
            />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              v-model="tableOptions.queryForm.phone"
              placeholder="手机号"
            />
          </el-form-item>
          <el-form-item label="账号状态" prop="status">
            <easy-dict-select
              ref="userStatusRef"
              :default-value="tableOptions.queryForm.status"
              dict-code="1000"
              show-all="Y"
              @change="handleStatusChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
            >
              重置
            </el-button>
            <el-button icon="search" type="primary" @click="queryTableData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用户账号"
        prop="userCode"
        width="140"
        sortable
      />
      <el-table-column
        align="center"
        label="用户名称"
        prop="userName"
        width="180"
        sortable
      />
      <el-table-column
        align="center"
        :formatter="formatBingUser"
        label="绑定员工"
        prop="user"
        width="100"
      />
      <el-table-column
        align="center"
        label="手机"
        prop="phone"
        width="120"
        sortable
      />
      <el-table-column
        align="center"
        label="工号"
        prop="labor"
        width="80"
        sortable
      />
      <el-table-column
        align="center"
        label="所属角色"
        prop="roleNames"
        width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户状态"
        prop="statusDesc"
        width="100"
      />
      <el-table-column
        align="center"
        label="用户类别"
        prop="userTypeDesc"
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="dateFormat"
        label="有效期限"
        prop="validDate"
        width="120"
      />
      <el-table-column
        align="center"
        label="是否可删除"
        prop="ifAllowDelete"
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="dateFormat"
        label="创建时间"
        prop="createTime"
        width="120"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close">
        取消
      </el-button>
      <el-button icon="circle-check" type="primary" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script setup name="UserPicker">
import { useMessage } from "@/hooks/message";
import EasyDictSelect from "@/components/dictSelect.vue";
import { queryUserTable } from "@/api/admin/org/sysuser/";
import { dateFormat } from "@/utils/common";
import { useTable } from "@/hooks/table";

const props = defineProps({
  title: {
    type: String,
    default: "选择用户"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  },
  filterKey: {
    type: Array,
    default: function () {
      return [];
    }
  },
  singleSelect: {
    type: Boolean,
    default: function () {
      return true;
    }
  }
});
const emits = defineEmits(["change"]);

const tableRef = ref(null);

const state = reactive({
  singleSelect: props.singleSelect,
  tableRef: null,
  userStatusRef: null,
  dialogVisible: false
});

const initFilter = () => {
  tableOptions.dataList = tableOptions.dataList.filter(item => {
    return props.filterKey.indexOf(item.id) < 0;
  });
};

const tableOptions = reactive({
  queryForm: {
    userName: "",
    userCode: "",
    phone: "",
    status: "0"
  },
  multiple: state.singleSelect,
  queryFunction: queryUserTable,
  descs: ["create_time"],
  onLoaded: initFilter
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const formatBingUser = row => {
  if (row.user) {
    return row.user.userName;
  } else {
    return "未绑定";
  }
};

const handleStatusChange = val => {
  tableOptions.queryForm.status = val;
  queryTableData();
};

const show = (singleSelect, params) => {
  if (params) {
    Object.assign(tableOptions.queryForm, {}, params);
  }
  if (singleSelect) {
    state.singleSelect = singleSelect;
  }
  queryTableData();
  state.dialogVisible = true;
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

const save = () => {
  emits("change", tableOptions.dataSelections);
  close();
};

defineExpose({
  save,
  show,
  close
});
</script>
<style lang="scss">
.el-container {
  flex-direction: column;
}
</style>
